// pages/accidentAssi/accidentAssi.js
import config from "../../../config.js";
var util = require('../../../utils/util.js');
const openIdUrl = config;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //城市列表
    user: null,
    car: null,
    cityList: ["西安", "咸阳"],
    cityIndex: 0,
    serviceType: 1,
    date: util.getDate(),
    mindate: util.getDate(),
    time: util.getMinute(),
    mintime: util.getMinute(),
    date2: util.getDate(),
    time2: util.getMinute(),
    arriveAdr: "",
    arriveAdr2: "",
    termList: [],
    termIndex: 0,
    termList2: [],
    termIndex2: 0,
    personList: [1, 2, 3],
    personIndex: 0,
    packageList: [1, 2, 3],
    packageIndex: 0,
    aiportCarList: [],
    aiportAllCarList: [],
    aiportOneCarList: [],
    airportCarIndex: 0,
    // banner轮播
    swiper: {
      autoplay: false,
      indicatorDots: false,
      img: [
        "../../image/banner.jpg",
        "../../image/banner.jpg",
        "../../image/banner.jpg"
      ]
    },
    currentIndex: 0,
    numId: 1,
    numId2: 1,
    packageId: 1,
    packageId2: 1,
    orderFrom: 2,
    locLatitude: 0,
    locLongitude: 0,
    addressPick: "",
    addressPick2: "",
    terminalId: 0,
    terminalId2: 0,
    flightNo2: "",
    flightNo: "",
    userId: null,
    orderId: -1,
    order: null,
    canChange:1
  },

  cityChange: function(e) {
    this.setData({
      cityIndex: e.detail.value,
    })
  },

  dateChange: function(e) {
    var time = util.getMinute();
    var date = e.detail.value;
    var currentDate = util.getDate();
    var mintime = this.data.mintime;



    if (date != currentDate) {
      mintime = "";
    } else {
      mintime = util.getMinute();
      this.setData({
        time: time
      })
    }


    this.setData({
      date: e.detail.value,
      mintime: mintime,
    })
  },

  timeChange: function(e) {
    this.setData({
      time: e.detail.value,
    })
  },

  makeCall: function() {
    wx.makePhoneCall({
      phoneNumber: "88888888",
    })
  },

  selectCustomer: function() {
    wx.navigateTo({
      url: '../customerList/customerList',
    })
  },

  getTermList: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })

    wx.request({
      url: openIdUrl.terminalList,
      data: {
        start: 0,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          console.log(res.data.data);
          that.setData({
            termList: res.data.data,
            termList2: res.data.data
          })

        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message + "",
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }
      }
    })
  },

  chooseMap: function() {
    var that = this;
    wx.chooseLocation({
      success: function(res) {
        console.log(res);
        that.setData({
          locLatitude: res.latitude,
          locLongitude: res.longitude,
          addressPick: res.address,
          arriveAdr: res.address

        })
      },
    })
  },
  chooseMap2: function() {
    var that = this;
    wx.chooseLocation({
      success: function(res) {
        console.log(res);
        that.setData({
          locLatitude: res.latitude,
          locLongitude: res.longitude,
          addressPick: res.address,
          arriveAdr2: res.address

        })
      },
    })
  },
  cityChange: function(e) {
    this.setData({
      cityIndex: e.detail.value,
    })
  },

  termChange: function(e) {
    this.setData({
      termIndex: e.detail.value,
    })
    console.log(this.data.termIndex);
  },
  termChange2: function(e) {
    this.setData({
      termIndex2: e.detail.value,
    })
    console.log(this.data.termIndex);
  },

  adrChange: function(e) {
    this.setData({
      arriveAdr: e.detail.value,
    })
  },
  adrChange2: function(e) {
    this.setData({
      arriveAdr2: e.detail.value,
    })
  },
  flightNoChange: function(e) {
    this.setData({
      flightNo: e.detail.value,
    })
  },
  terminalIdChange: function(e) {
    this.setData({
      terminalId: e.detail.value,
    })
  },
  numIdChange: function(e) {
    this.setData({
      personIndex: e.detail.value,
    })
  },
  packageIdChange: function(e) {
    this.setData({
      packageIndex: e.detail.value,
    })
  },

  timeChange: function(e) {
    this.setData({
      time: e.detail.value,
    })
  },

  flightNo2Change: function(e) {
    this.setData({
      flightNo2: e.detail.value,
    })

    console.log(this.data.flightNo2);
  },
  terminalId2Change: function(e) {
    this.setData({
      terminalId2: e.detail.value,
    })
  },
  numId2Change: function(e) {
    this.setData({
      numId2: e.detail.value,
    })
  },
  packageId2Change: function(e) {
    this.setData({
      packageId2: e.detail.value,
    })
  },
  date2Change: function(e) {
    this.setData({
      date2: e.detail.value,
    })
  },

  time2Change: function(e) {
    this.setData({
      time2: e.detail.value,
    })
  },

  makeCall: function() {
    wx.makePhoneCall({
      phoneNumber: "88888888",
    })
  },

  nextStep: function() {
    wx.showModal({
      title: "恭喜您",
      content: "预约成功，车管家将会在10分钟内通过电话与您沟通详情 ，请保持电话畅通！谢谢！",
      showCancel: false,
      confirmColor: "#137AF0",
      success: function() {
        wx.switchTab({
          url: "../order/order",
        })
      }
    })
  },
  submit: function() {
    console.log(this.data.serviceType);
    if (this.data.serviceType == 1) {
      this.updateSubmitData();
    } else {
      this.updateSubmitData2();
    }

  },
  //送机
  updateSubmitData2: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    var user = wx.getStorageSync("user");
    console.log(user);


    var comId = 0;
    console.log(that.data);
    var addressReturn = that.data.arriveAdr;
    if (that.data.user == null) {
      wx.showToast({
        title: "请选择客户及车辆",
        icon: 'none',
        mask: true,
        duration: 2000
      })
      return false;
    }
    if (that.data.arriveAdr2 == "") {
      wx.showToast({
        title: "请输入到达地点",
        icon: 'none',
        mask: true,
        duration: 2000
      })
      return false;
    }
    if (that.data.numId2 == "") {
      wx.showToast({
        title: "请输入接机人数",
        icon: 'none',
        mask: true,
        duration: 2000
      })
      return false;
    }
    console.log(addressReturn);
    var tel = user.tel;

    console.log(tel);
    var returnTime = that.data.date + " " + that.data.time;
    console.log(returnTime);
    var ternId = that.data.termList2[that.data.termIndex2].terminalId;
    wx.request({
      url: openIdUrl.airportSubbmit,
      data: {
        // orderId: that.data.order.order.housekeeper.keeperId,
        orderId: that.data.orderId,
        userId: that.data.user.aid,
        orderType: 6,
        terminalId: ternId,
        numId: that.data.personList[that.data.personIndex],
        packageId: that.data.packageList[that.data.packageIndex],
        orderFrom: that.data.orderFrom,
        locLatitude: that.data.locLatitude,
        locLongitude: that.data.locLongitude,
        address: that.data.arriveAdr2,
        flightNo: that.data.flightNo2,
        tel: tel,
        serviceType: that.data.serviceType,
        pickTime: that.data.date2 + " " + that.data.time2,
        airportCarId: that.data.airportCarIndex
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          wx.setStorageSync("orderId", res.data.data.oid);
          wx.setStorageSync("orderConfirmType", 1);
          wx.reLaunch({
            url: "../gjorder/gjorder",
          })
          wx.showLoading({
            title: '订单更改成功！',
          })

        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message + "",
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }

      }
    })
  },
  updateSubmitData: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    var user = wx.getStorageSync("user");
    console.log(user);


    var comId = 0;
    console.log(comId);
    var addressReturn = that.data.arriveAdr;
    if (that.data.arriveAdr == "") {
      wx.showToast({
        title: "请输入到达地点！",
        icon: 'none',
        mask: true,
        duration: 2000
      })
      return false;
    }
    if (that.data.numId == "") {
      wx.showToast({
        title: "请输入接机人数",
        icon: 'none',
        mask: true,
        duration: 2000
      })
      return false;
    }
    if (that.data.flightNo == "") {
      wx.showToast({
        title: "请输入航班号",
        icon: 'none',
        mask: true,
        duration: 2000
      })
      return false;
    }
    console.log(addressReturn);
    var tel = user.tel;

    console.log(tel);
    var returnTime = that.data.date + " " + that.data.time;
    console.log(returnTime);
    var ternId = that.data.termList[that.data.termIndex].terminalId;
    wx.request({
      url: openIdUrl.airportSubbmit,
      data: {
        houseKeeperId: that.data.order!=null?that.data.order.order.housekeeper.keeperId:-1,
        orderId: that.data.orderId,
        userId: that.data.user.aid,
        orderType: 6,
        terminalId: ternId,
        numId: that.data.personList[that.data.personIndex],
        packageId: that.data.packageList[that.data.packageIndex],
        orderFrom: that.data.orderFrom,
        locLatitude: that.data.locLatitude,
        locLongitude: that.data.locLongitude,
        address: that.data.arriveAdr,
        flightNo: that.data.flightNo,
        tel: tel,
        serviceType: that.data.serviceType,
        pickTime: that.data.date + " " + that.data.time,
        airportCarId: that.data.airportCarIndex
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          wx.setStorageSync("orderId", res.data.data.oid);
          wx.setStorageSync("orderConfirmType", 1);
          wx.reLaunch({
            url: "../gjorder/gjorder",
          })
          wx.showLoading({
            title: '订单更改成功！',
          })

        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message + "",
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }

      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var canChange = options.canChange;
    that.setData({
      canChange: canChange,
      date:util.getDate(),
      mindate:util.getDate(),
      time:util.getMinute(),
      mintime:util.getMinute(),
    })

    //判断是否是再次下单或者更改订单类型
    var orderId = options.orderId;
    var userId = options.uid;

    console.log(orderId);
    console.log(userId);
    if (orderId != null) {
      that.setData({
        orderId: orderId
      })

      //获取订单详情
      that.orderDetail();
    }


  },


  orderDetail() {
    var that = this;
    //获取订单详情
    wx.request({
      url: openIdUrl.orderDetailUrl,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        id: this.data.orderId,
        isKeeper: 1
      },
      method: "POST",
      success: function(res) {
        console.log(res.data);
        if (res.data.status == 200) {
          that.setData({
            order: res.data.data,
            user: res.data.data.order.account,
            date: res.data.data.service.connectionTime.substr(0,10)
          })
          that.setOrderData();
        } else {
          wx.showToast({
            title: '订单获取失败！',
            icon: 'none',
            duration: 1500
          })
          return false;
        }
      }

    })
  },
  setOrderData() {
    var that = this;
    //订单信息
    var order = that.data.order;
    var serviceType = order.service.serviceAirportType.typeId;
    if (serviceType == 1) {
      that.setData({
        // typeIndex: order.service.insuranceCom.insuranceId - 1,
        currentIndex: 0,
        user: order.order.account,
        car: order.order.cars,
        flightNo: order.service.flightNo,
        addressPick: order.service.location.name,
        arriveAdr: order.service.location.name,
        terminalId: order.service.airportTerminal.airport.airportId,
        packageIndex: order.service.packeage.packageId - 1,
        personIndex: order.service.usersNum.numId - 1,

      })
    } else {
      that.setData({
        currentIndex: 1,
        serviceType: serviceType,
        user: order.order.account,
        car: order.order.cars,
        flightNo2: order.service.flightNo,
        addressPick2: order.service.location.name,
        arriveAdr2: order.service.location.name,
        terminalId2: order.service.airportTerminal.airport.airportId,
        packageIndex: order.service.packeage.packageId - 1,
        personIndex: order.service.usersNum.numId - 1,

      })
    }
    console.log(order);
    // var service = that.data.order.service;

    // console.log(that.data.typeIndex);
    console.log(that.data.user);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getTermList();
    this.getAirPortCarList();
    var item = wx.getStorageSync("userchoose");
    console.log(item);
    if (item != null) {
      this.setData({
        user: item
      })
    }

    var car = wx.getStorageSync("carchoose");
    if (car != null) {
      this.setData({
        car: car
      })
    }
  },
  exchangeCar: function(e) {
    var userId = e.currentTarget.dataset.userid;
    if (userId) {
      wx.navigateTo({
        url: '../carList/carList?userId=' + userId,
      })
    }
  },
  exchangeCustomer: function(e) {
    wx.navigateTo({
      url: '../customerList/customerList',
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    wx.setStorageSync("userchoose", null);
    wx.setStorageSync("carchoose", null);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 改变事件
   */
  intervalChange: function(e) {
    console.log("changed!" + e.detail.current);
    this.setData({
      currentIndex: e.detail.current
    });
  },
  /**
   * 切换事件
   */
  switchAction(event) {
    if (event.currentTarget.id == 'item1') {
      this.setData({
        currentIndex: 0,
        serviceType: 1
      });
    } else {
      this.setData({
        currentIndex: 1,
        serviceType: 2
      });
    }

  },
  loadMap() {
    wx.navigateTo({
      url: '../map/map',
    })
  },
  numIdChange: function (e) {
    var that = this;
    var item = that.data.personList[e.detail.value];
    console.log(item);
    var aiportCars = that.data.aiportAllCarList;
    if (item != 1) {
      aiportCars = that.data.aiportOneCarList;
    }

    //设置数据
    this.setData({
      airportCarIndex: 0,
      personIndex: e.detail.value,
      aiportCarList: aiportCars
    })

  },
  numId2Change: function (e) {
    this.setData({
      numId2: e.detail.value,
    })
  },
  packageId2Change: function (e) {
    this.setData({
      packageId2: e.detail.value,
    })
  },
  packageIdChange: function (e) {
    this.setData({
      packageIndex: e.detail.value,
    })
  },
  carIdChange: function (e) {
    this.setData({
      airportCarIndex: e.detail.value,
    })
  },
  selectCustomer: function(e) {
    wx.navigateTo({
      url: '../customerList/customerList',
    })
  },
  exchangeCar: function(e) {
    var id = e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: '../carList/carList?userId=' + id,
    })
  },
  getAirPortCarList: function () {
    var that = this;
    wx.request({
      url: openIdUrl.airportCarLocList,
      data: {
        start: 0,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 200) {
          console.log(res.data.data);
          if (res.data.data.length > 0) {
            var itemOne = [res.data.data[0]];
            that.setData({
              aiportCarList: res.data.data,
              aiportAllCarList: res.data.data,
              aiportOneCarList: itemOne,
            })
          }


        }
      }
    })
  },
})